// 数组对象去重
export function removeDuplicateObjects(array, prop) {
  const result = {};
  const finalResult = [];
  for (let i = 0; i < array.length; i++) {
    result[array[i][prop]] = array[i];
  }
  for (const item in result) {
    finalResult.push(result[item]);
  }
  return finalResult;
}

export function formatThousands(num, substitution = '-') {
  if (!num && num !== 0) return substitution;
  const numToString = `${num}`;
  let strFront = '';
  let strEnd = '';
  let meetPoint = false;
  for (let i = 0; i < numToString.length; i++) {
    if (numToString[i] === '.') {
      meetPoint = true;
    }
    meetPoint ? (strEnd += numToString[i]) : (strFront += numToString[i]);
  }
  let result = '';
  strEnd
    ? (result = `${strFront.replace(
      /\d{1,3}(?=(\d{3})+(\.\d*)?$)/g,
      '$&,'
    )}.${strEnd.replace('.', '')}`)
    : (result = strFront.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,'));
  return result;
}
/* 判断pc浏览器是否缩放，若返回100则为默认无缩放，如果大于100则是放大，否则缩小 */
export function detectZoom() {
  let ratio = 0;
  const screen = window.screen;
  const ua = navigator.userAgent.toLowerCase();

  if (window.devicePixelRatio !== undefined) {
    ratio = window.devicePixelRatio;
  } else if (~ua.indexOf('msie')) {
    if (screen.deviceXDPI && screen.logicalXDPI) {
      ratio = screen.deviceXDPI / screen.logicalXDPI;
    }
  } else if (
    window.outerWidth !== undefined &&
    window.innerWidth !== undefined
  ) {
    ratio = window.outerWidth / window.innerWidth;
  }

  if (ratio) {
    ratio = Math.round(ratio * 100);
  }
  return ratio;
}
/**
 * @Descripttion: 导出二进制的文件
 * @param {*} result
 * @param {*} fileName
 * @return {*}
 */
export function exportFile(result, fileName, type) {
  const blob = result instanceof Blob ? result : new Blob([result], { type: type });
  if ('download' in document.createElement('a')) { // 非IE下载
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
    window.URL.revokeObjectURL(link.href); // 释放URL 对象
    document.body.removeChild(link);
  } else { // IE10+下载
    navigator.msSaveBlob(blob, fileName);
  }
}
/**
 * 判断接口响应参数是否是blob文件流
 * @param {*} responseResult xhr请求响应参数
 */
export const judgeResponseType = function(responseResult) {
  return new Promise((resolve, reject) => {
    if (responseResult.type === 'application/json') {
      // 说明是普通对象数据，后台转换失败
      const reader = new FileReader();
      reader.readAsText(responseResult, 'utf-8');
      reader.onload = () => {
        const jsonData = JSON.parse(reader.result);
        reject(jsonData);
      };
    } else {
      // 说明是正常的文件流
      resolve();
    }
  });
};
/**
 * @name 判断是否为Number或String类型---排除非数字假值
 * @params 字符串数字或则数字
  */
export const isNS = str => {
  if (Object.prototype.toString.call(str) === '[object Number]') {
    return String(str);
  } else if (Object.prototype.toString.call(str) === '[object String]' && str !== '' && Object.prototype.toString.call(Number(str)) === '[object Number]') {
    return str;
  } else return false;
};
